﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using POSSystem.BLL;

namespace WebUI
{
    public partial class BranchList : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            base.Page_Load(sender, e);
            lstBranch.OnNeedDataSource = lstBranch_OnNeedDataSource;
            lstBranch.OnClientClick = "lstBranch_OnClientClick();";
            if (!IsPostBack)
            {
                SearchFilter = Branches.ColumnNames.Deleted + " = 0";
                lstBranch.Refresh();
            }
        }

        public string SearchFilter
        {
            get { return ViewState["SearchFilter"] == null ? "" : ViewState["SearchFilter"].ToString(); }
            set { ViewState["SearchFilter"] = value; }
        }

        protected void lstBranch_OnNeedDataSource(Object sender, EventArgs e)
        {
            Branches branch = new Branches();
            string cols = "*";
            string sort = lstBranch.SortExpression + " " + lstBranch.SortDirection;
            int totalRow = branch.Search(SearchFilter, cols, sort.Trim(), lstBranch.CurrentPageIndex * lstBranch.PageSize, lstBranch.PageSize);
            lstBranch.VirtualItemCount = totalRow;
            lstBranch.DataSource = branch.DefaultView;
        }

        protected void btnAddBranch_OnClick(object sender, EventArgs e)
        {
            lstBranch.Refresh();
            WebUI.Application master = (WebUI.Application)this.Master;
            master.LoadBranches();
        }

        protected void btnRemoveBranch_OnClick(object sender, EventArgs e)
        {
            lstBranch.DeleteCheckedItems();
            lstBranch.Refresh();
        }

        public string GetBranchEditPageLink()
        {
            return "EditPages/EditBranch.aspx?";
        }

        protected void btnLoadByBranchID_OnClick(object sender, EventArgs e)
        {
            int branchID = int.Parse(hidSelectedBranchID.Value);
            BranchRole role = BranchRole.GetBranchRoleByBranchID(branchID);
            if (role.RowCount == 0)
            {
                Branches branch = new Branches();
                branch.LoadByPrimaryKey(branchID);

                role.AddNew();
                role.Sales = true;
                role.Purchase = true;
                role.Products = true;
                role.Report = true;
                role.Repair = true;
                role.Customers = true;
                role.Suppliers = true;
                role.Admin = true;
                role.SystemAdmin = branch.IsHeadQuarter;
                role.Contracts = true;
                role.EditSales = true;
                role.BranchID = branchID;
                role.Expense = true;
            }
            lstRole.RoleList = role;
            lstRole.Refresh();
        }

        protected void btnSaveRole_OnClick(object sender, EventArgs e)
        {
            lstRole.Update();
        }
    }
}